package org.lds.logger.config.appender.async;

import org.lds.logger.config.appender.Appender;
import org.lds.logger.event.LoggerEvent;

import java.util.ArrayList;
import java.util.List;

public class SingleThreadAsyncAppenderTask implements Runnable {
    private AbstractAsyncAppenderGroup asyncAppenderGroup;

    public SingleThreadAsyncAppenderTask(AbstractAsyncAppenderGroup asyncAppenderGroup) {
        this.asyncAppenderGroup = asyncAppenderGroup;
    }

    @Override
    public void run() {
        while (true) {
            consumer();
        }
    }

    private void consumer() {
        List<LoggerEvent> loggerEvents = new ArrayList<>();
        asyncAppenderGroup.getQueue().drainTo(loggerEvents);

        for (Appender appender : asyncAppenderGroup.getAsyncAppenderList()) {
            appender.append(loggerEvents);
        }
    }

}
